package com.road.service.imp;

import java.sql.SQLException;

import com.road.dao.IUserDao;
import com.road.dao.imp.UserDaoImp;
import com.road.model.User;
import com.road.service.IUserService;
import com.road.utils.MailUtil;

public class UserServiceImp implements IUserService {

	//将用户保存到数据库
	IUserDao userDao=new UserDaoImp();
	@Override
	public boolean doRegister(User user) throws SQLException {
		
		//保存成功则通过线程的方式给用户发送一封邮件
		if(userDao.save(user)>0){
              
			if(user.getEmail()!=null) {
				new Thread(new MailUtil(user.getEmail(), user.getCode(),user.getUser_pass(),"","","")).start();;
			}
			
			return true;
		}

		return false;

	}



	@Override

	public boolean activeUser(String code) throws SQLException {

		if(userDao.activeUser(code)>0){

			return true;

		}else{

			return false;

		}

	}



	@Override
	public boolean cheakUserByEmail(User user) throws SQLException {
		// TODO Auto-generated method stub
		if(userDao.getUserByEmail(user)>0){

			return true;

		}else{

			return false;

		}
	}
	
	@Override
	public boolean cheakEmail(String email)  {
		// TODO Auto-generated method stub
			//利用正则表达式（可改进）验证邮箱是否符合邮箱的格式
			if(!email.matches("^\\w+@(\\w+\\.)+\\w+$")){
				return false;
			}
			return true;

	}



	@Override
	public boolean cheakToken(User user) {
		boolean isok=false;
		try {
			int	result=userDao.getUserByToken(user);
			if (result>0) {
				isok=true;	
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return isok;
	}



	@Override
	public User login(User user) {
		// TODO Auto-generated method stub
		User us=null;
		try {
			us=userDao.login(user);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return us;
	}



	@Override
	public boolean getUserByPhone(User user) {
		// TODO Auto-generated method stub
		try {
			return userDao.getUserByPhone(user);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}



	@Override
	public boolean updateUserIcon(User user, String url) throws SQLException {
		// TODO Auto-generated method stub
		//更新用户头像
		return userDao.updateUserIcon(user, url);
	}
	
	
	
}
